﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;


namespace HelloASPNET
{
  public partial class DangNhap : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnTaoTaiKhoan_Click(object sender, EventArgs e)
    {
      //Ket noi voi CSDL
      SqlConnection cnn = new SqlConnection();
      cnn.ConnectionString = @"Data Source=.;Initial Catalog=Demo1;Integrated Security=True"; //
      cnn.Open();
      //Thuc hien cau lenh sql
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = cnn;  //gan ket noi cho command
      string sqlInsert ="INSERT INTO NguoiDung(Username,[Password],HoTen,GioiTinh,NgaySinh)  VALUES(@Username,@MatKhau,@HoTen,@GioiTinh,@NgaySinh)";
      //Cau lenh sql co the chua cac tham so, cac tham so co ten bat dau = @
      //Truoc khi Execute cau lenh, thi phai cung cap gia tri cho tat ca cac tham so.
      cmd.CommandText = sqlInsert;
      //cung cap gia tri cho cac tham so
      cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
      cmd.Parameters.AddWithValue("@MatKhau", txtPassword.Text);
      cmd.Parameters.AddWithValue("@HoTen", txtHoTen.Text);
      cmd.Parameters.AddWithValue("@GioiTinh", radGioiTinhNam.Checked);
      DateTime ngaySinh = DateTime.Parse(txtNgaySinh.Text); //Chuyen noi dung chuoi thanh kieu DateTime
      cmd.Parameters.AddWithValue("@NgaySinh", ngaySinh);
      int count = cmd.ExecuteNonQuery();

      lblThongBao.Text = "So row da insert thanh cong : " + count.ToString();

      cnn.Close();//Dong ket noi
    }
  }
}